# coding = utf-8

import numpy as np
import pandas as pd
from pandas import Series, DataFrame

s1 = Series( np.arange( 3 ), index = [ "A", "B", "C" ] )
s2 = Series( np.arange( 3 ), index = [ "D", "E", "F" ] )
# 将 s1,s2 连接起来
s3 = pd.concat( [ s1, s2 ] )
print( s3 )
'''
A    0
B    1
C    2
D    0
E    1
F    2
'''
# 将 axis 赋值为 1
s3 = pd.concat( [ s1, s2 ], axis = 1 )
print( s3 )
''' 相当于 转换成了 DataFrame
     0    1
A  0.0  NaN
B  1.0  NaN
C  2.0  NaN
D  NaN  0.0
E  NaN  1.0
F  NaN  2.0
'''

df1 = DataFrame( np.random.randn(12).reshape( 4, 3 ), columns = [ 'A', 'B', 'C' ] )
df2 = DataFrame( np.random.randn(9).reshape( 3, 3 ), columns = [ 'A', 'B', 'Z' ] )
print( df1 )
print( df2 )
df3 = pd.concat( [ df1, df2 ] )
print( df3 )
''' 按照按照行列，全部拼接在一起，缺失的值用nan填充
          A         B         C         Z
0 -1.318278  0.706190 -1.736900       NaN
1 -0.805286  1.038098  0.721436       NaN
2 -0.838711 -0.026205 -0.050671       NaN
3 -1.687591  0.370406  2.151407       NaN
0 -0.709602  0.012840       NaN  1.351957
1 -0.671164  0.190570       NaN  0.315375
2  1.200198 -1.729294       NaN  1.163809
'''

# Series 的 conbine
s1 = Series( [ 2, np.nan, 4, np.nan ], index = [ 'A', 'B', 'C', 'D' ] )
s2 = Series( [ 1, 2, 3, 4 ], index = [ 'A', 'B', 'C', 'D' ] )

s3 = s1.combine_first( s2 )
print( s3 )
''' combine_first 的作用：使用s2的内容，去填充s1的nan
A    2.0
B    2.0
C    4.0
D    4.0
'''

df1 = DataFrame( {
    "X": [ 1, np.nan, 3, np.nan ],
    "Y": [ 5, np.nan, 7, np.nan ],
    "Z": [ 9, np.nan, 11, np.nan ]
} )

df2 = DataFrame( {
    "Z": [ np.nan, 10, np.nan, 12 ],
    "A": [ 1, 2, 3, 4 ]
} )

df3 = df1.combine_first( df2 )
print( df3 )
''' 按照相同的列，才能填充
     A    X    Y     Z
0  1.0  1.0  5.0   9.0
1  2.0  NaN  NaN  10.0
2  3.0  3.0  7.0  11.0
3  4.0  NaN  NaN  12.0
'''